//
//  StatusPhotosView.m
//  微博2期
//
//  Created by J on 15/12/5.
//
//

#import "StatusPhotosView.h"
#import "Photo.h"
#import "PhotoView.h"
#define StatusPhotoMaxCol(count) ((count==4)?2.0:3.0);
@implementation StatusPhotosView


//** 计算配图尺寸*/
+ (CGSize)sizeWithCount:(NSUInteger)count{
    NSUInteger maxCols = 3.0;
    //列数
    NSUInteger cols = (count >= maxCols) ? maxCols :count;
    CGFloat photosW = cols * StatusPhotoWH + (cols - 1) * StatusPhotoMargin;
    //行数
    NSUInteger rows = ceilf(count/3);
        //    int rows = (count + maxCols -1)/maxCols;
    CGFloat photosH = rows * StatusPhotoWH + (rows - 1) * StatusPhotoMargin;
    return CGSizeMake(photosW, photosH);
    
}



- (void)setPhotos:(NSArray *)photos{
    _photos = photos;
    NSUInteger photoCount= photos.count;
    //创建足够数量的图片控件
        while (self.subviews.count < photoCount) {//只要不够就循环一次创建一个
            PhotoView * photoView = [PhotoView new];
            [self addSubview:photoView];
        }
    
        //遍历所有图片子控件 设置图片
    for (int i=0; i<self.subviews.count; i++) {
        PhotoView * photoView = self.subviews[i];
        if (i < photoCount) {//显示
            photoView.photo = photos[i];//取出数据
            photoView.hidden = NO;
          
        }else{//隐藏
            photoView.hidden = YES;
            
        }
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    //设置图片尺寸和位置
    NSUInteger photoCount= self.photos.count;
    NSUInteger maxCol = StatusPhotoMaxCol(self.photos.count)
    
    for (int i=0; i<photoCount; i++) {
        PhotoView * photoView = self.subviews[i];
        NSUInteger col = i%maxCol;
        photoView.x = col*(StatusPhotoWH + StatusCellMargin);
        
        NSUInteger row = i/maxCol;
        photoView.y = row*(StatusPhotoWH + StatusCellMargin);

        photoView.width = StatusPhotoWH;
        photoView.height = StatusPhotoWH;

    }
}


    

@end
